package com.easondongh.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easondongh.business.domain.*;
import com.easondongh.business.mapper.GoodsImportMapper;
import com.easondongh.business.mapper.GoodsMapper;
import com.easondongh.business.mapper.ProviderMapper;
import com.easondongh.business.service.GoodsImportService;
import com.easondongh.business.service.GoodsService;
import com.easondongh.business.service.ProviderService;
import com.easondongh.business.vo.CustomerQueryVo;
import com.easondongh.business.vo.GoodsBackQueryVo;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.business.mapper.GoodsBackMapper;
import com.easondongh.business.service.GoodsBackService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author EasonDongH
 * @date 2020/6/2 10:42
 */
@Service
public class GoodsBackServiceImpl extends ServiceImpl<GoodsBackMapper, GoodsBack> implements GoodsBackService{

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsImportMapper goodsImportMapper;
    @Autowired
    private GoodsBackMapper goodsBackMapper;
    @Autowired
    private ProviderMapper providerMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void processGoodsBack(GoodsBack goodsBack) {
        GoodsImport importData = this.goodsImportMapper.selectById(goodsBack.getImportId());
        importData.setNumber(Math.max(0, importData.getNumber() - goodsBack.getNumber()));
        this.goodsImportMapper.updateById(importData);
        this.goodsMapper.updateStock(goodsBack.getGoodsId(), 0-goodsBack.getNumber());
        this.goodsBackMapper.insert(goodsBack);
    }

    @Override
    public PageResult<GoodsBack> getByPage(BasePageQueryVo<GoodsBackQueryVo> queryCondition) {
        IPage<GoodsBack> page = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        QueryWrapper<GoodsBack> queryWrapper = new QueryWrapper<>();
        GoodsBackQueryVo queryParams = queryCondition.getQueryParams();
        if (queryParams != null) {
            queryWrapper.eq(queryParams.getGoodsId() != null, GoodsBack.COL_GOODS_ID, queryParams.getGoodsId());
            queryWrapper.eq(queryParams.getProviderId() != null, GoodsBack.COL_PROVIDER_ID, queryParams.getProviderId());
            queryWrapper.gt(queryParams.getStartTime() != null, GoodsBack.COL_BACK_TIME, queryParams.getStartTime());
            queryWrapper.lt(queryParams.getEndTime() != null, GoodsBack.COL_BACK_TIME, queryParams.getEndTime());
        }
        queryWrapper.orderByDesc(GoodsBack.COL_BACK_TIME);
        IPage<GoodsBack> iPage = this.goodsBackMapper.selectPage(page, queryWrapper);
        List<GoodsBack> goodsBackList = iPage.getRecords();
        if(!CollectionUtils.isEmpty(goodsBackList)) {
            List<Provider> providerList = this.providerMapper.getAllEnableProvider();
            List<Goods> goodsList = this.goodsMapper.getAllEnableGoods();
            Map<Integer, String> providerMap = new HashMap<>();
            Map<Integer, String> goodsMap = new HashMap<>();
            if(!CollectionUtils.isEmpty(providerList)) {
                providerMap = providerList.stream().collect(Collectors.toMap(Provider::getId, Provider::getProviderName));
            }
            if(!CollectionUtils.isEmpty(goodsList)) {
                goodsMap = goodsList.stream().collect(Collectors.toMap(Goods::getId, Goods::getGoodsName));
            }
            for (GoodsBack goodsBack : goodsBackList) {
                goodsBack.setGoodsName(goodsMap.get(goodsBack.getGoodsId()));
                goodsBack.setProviderName(providerMap.get(goodsBack.getProviderId()));
            }
        }

        return new PageResult<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), goodsBackList);
    }
}
